package com.spt.ws.client;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;

import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;

import com.spt.core.Property;

public class WebServiceBase {
	public String callWS(String mothed,String parm) {
		try{
		RPCServiceClient serviceClient = new RPCServiceClient();
		Options options = serviceClient.getOptions();
		EndpointReference targetEPR = new EndpointReference(Property.getProperty("endpoint"));
		options.setTo( targetEPR );
		options.setTimeOutInMilliSeconds( 90000 );
		QName opName = new QName(Property.getProperty("namespaceuri"), mothed);
		Object[] results = serviceClient.invokeBlocking( opName, new Object[]{parm},  new Class[] {String.class} );
		String res = (String)results[0];
		return res;
		}catch(Exception e){
			e.printStackTrace();
			return e.getMessage();
		}
	}
	
	protected boolean checkMsg(String msg){
        if(msg.startsWith("#MSG")&&msg.endsWith("#END")){
            String s[] = msg.split("::");
            if("0".equals(s[1])) {
                return true;
            }else {
                return false;
            }
            
        }else{
            return false;
        }
    }
    
    protected String assembleLine(String[] keys,Map<String,Object> map){
        StringBuffer sb = new StringBuffer("::");
        for(String key:keys){
            sb.append(map.get(key)).append("::");
        }
        sb.append("||");
        return sb.toString();
    }
    protected List<String> assemb(String parm){
        List<String> li = new ArrayList<String>();
        String[] parmLine = parm.split("\\|\\|");
        for (String ss : parmLine){
            if(ss.startsWith("#HEAD::")) {
                li.add(ss+"||#END");
            }else if(ss.startsWith("#END")){
                continue;
            }else{
                li.add("#HEAD" + ss+"||#END");
            }
        }
        return li;
    }
}
